﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace SmartMathLibrary.IO
{
    /// <summary>
    /// This abstract class provides methods to build up a specialized file writer for xml documents.
    /// </summary>
    [Serializable]
    public abstract class AbstractXmlFileWriter
    {
        /// <summary>
        /// The primary xml writer engine.
        /// </summary>
        protected XmlTextWriter writeEngine;

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractXmlFileWriter"/> class.
        /// </summary>
        /// <param name="stream">The stream of the file writer.</param>
        /// <param name="encoding">The encoding of the file.</param>
        protected AbstractXmlFileWriter(Stream stream, Encoding encoding)
        {
            this.writeEngine = new XmlTextWriter(stream, encoding) {Formatting = Formatting.Indented};
            this.writeEngine.WriteStartDocument(false);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractXmlFileWriter"/> class.
        /// </summary>
        /// <param name="filename">The filename of the file to create.</param>
        /// <param name="encoding">The encoding of the file.</param>
        protected AbstractXmlFileWriter(String filename, Encoding encoding)
        {
            this.writeEngine = new XmlTextWriter(filename, encoding) {Formatting = Formatting.Indented};
            this.writeEngine.WriteStartDocument(false);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractXmlFileWriter"/> class.
        /// </summary>
        /// <param name="textWriter">The text writer of the writer.</param>
        protected AbstractXmlFileWriter(TextWriter textWriter)
        {
            this.writeEngine = new XmlTextWriter(textWriter) {Formatting = Formatting.Indented};
            this.writeEngine.WriteStartDocument(false);
        }

        /// <summary>
        /// Closes the writer and saves the output values.
        /// </summary>
        public virtual void Close()
        {
            this.writeEngine.Close();
        }
    }
}